package com.sky.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sky.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        log.info("开始插入填充...");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        if (ThreadLocalUtil.getUserInfo() != null) {
            Long id = ThreadLocalUtil.getUserInfo().getId();
            if (id != null) {
                this.strictInsertFill(metaObject, "createUser", Long.class, id);
                this.strictInsertFill(metaObject, "updateUser", Long.class, id);
            }
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新填充...");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        if (ThreadLocalUtil.getUserInfo() != null) {
            Long id = ThreadLocalUtil.getUserInfo().getId();
            if (id != null) {
                this.strictUpdateFill(metaObject, "updateUser", Long.class, id);
            }
        }
    }
}
